<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class BonusDetail extends Base
{
    public function initialize()
    {
        parent::initialize();
        //奖金名称
        $this->prize = config('extra_prize');
        View::assign('prize',config('extra_prize'));

        //级别
        $this->jjlevel = Db::name('user_level')->column('level_name','rank');
        View::assign('jjlevel',$this->jjlevel);
        $this->jjlevel1 = Db::name('user_level1')->column('level_name','rank');
        View::assign('jjlevel1', $this->jjlevel1);

    }

    /**
     * 奖金明细
     */
    public function index(){

        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['l.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['l.username'] = input('username');
            }
            if(input('level')){
                $map['l.level'] = input('level');
            }
            if(input('level1')){
                $map['l.level1'] = input('level1');
            }
            if(input('bonus_type')){
                $map['l.bonus_type'] = input('bonus_type');
            }
            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['l.w_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('bonus_detail')->alias('l')->where($map)->count();

            $field = 'l.*,u.username,u.nickname';
            $list = Db::name('bonus_detail')
                ->alias('l')
                ->join('users u','l.user_id=u.user_id')
                ->field($field)
                ->where($map)
                ->order('l.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                //$v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $v['bonus_type_name'] = $this->prize[$v['bonus_type']];
                $v['level_name'] = $this->jjlevel[$v['level']];
                $v['level1_name'] = $this->jjlevel1[$v['level1']];
                $v['status_str'] = $v['status']==1?'已发放':'未发放';
                $order_time = 0;
                if($v['data_table']=='orders' && $v['data_id']){
                    $order_time = Db::name('order')->where('order_id',$v['data_id'])->value('pay_time');
                }
                $v['order_time'] = $order_time?date('Y-m-d H:i:s',$order_time):'';
                $list[$k] = $v;
            }
            $total = Db::name('bonus_detail')
                ->alias('l')
                ->where($map)
                ->field('sum(bonus) total_money')
                ->find();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list,'totalRow'=>$total]);
        }
        $wallet_names = logic('Wallet')->getName();
        View::assign('wallet_names',$wallet_names);
        return View::fetch();
    }

    public function guanli_log(){

        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['l.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['l.username'] = input('username');
            }
            if(input('level')){
                $map['l.level'] = input('level');
            }
            if(input('level1')){
                $map['l.level1'] = input('level1');
            }
            if(input('bonus_type')){
                $map['l.bonus_type'] = input('bonus_type');
            }
            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['l.w_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('guanli_log')->alias('l')->where($map)->count();

            $field = 'l.*,u.username,u.nickname';
            $list = Db::name('guanli_log')
                ->alias('l')
                ->join('users u','l.user_id=u.user_id','left')
                ->field($field)
                ->where($map)
                ->order('l.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                //$v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $v['bonus_type_name'] = $this->prize[$v['bonus_type']];
                $list[$k] = $v;
            }
            $total = Db::name('guanli_log')
                ->alias('l')
                ->where($map)
                ->field('sum(guanli) total_money')
                ->find();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list,'totalRow'=>$total]);
        }
        return View::fetch();
    }

    public function shouyi_release(){

        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['l.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['l.username'] = input('username');
            }

            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['l.w_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('shouyi_release')->alias('l')->alias('l')->join('users u','l.user_id=u.user_id','left')->where($map)->count();

            $field = 'l.*,u.username,u.nickname';
            $list = Db::name('shouyi_release')
                ->alias('l')
                ->join('users u','l.user_id=u.user_id','left')
                ->field($field)
                ->where($map)
                ->order('l.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                //$v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $v['bonus_type_name'] = $this->prize[$v['bonus_type']];
                $list[$k] = $v;
            }
            $total = Db::name('guanli_log')
                ->alias('l')
                ->join('users u','l.user_id=u.user_id','left')
                ->where($map)
                ->field('sum(guanli) total_money')
                ->find();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list,'totalRow'=>$total]);
        }
        return View::fetch();
    }
    public function del_shouyi_release(){
        $ids = input('ids/a');
        Db::name('shouyi_release')->where(['id'=>['in',$ids]])->delete();
        return json(['status'=>1,'msg'=>'删除成功！']);
    }
    public function handle_shouyi_release(){
        $res = logic('Bonus')->jingtai_release();
        return json($res);
    }

    //董事分红激励
    public function fenhong_log(){

        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['l.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['l.username'] = input('username');
            }
            if(input('level1')){
                $map['l.level1'] = input('level1');
            }
            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['l.w_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('fenhong_log')->alias('l')->where($map)->count();

            $field = 'l.*,u.username,u.nickname';
            $list = Db::name('fenhong_log')
                ->alias('l')
                ->join('users u','l.user_id=u.user_id','left')
                ->field($field)
                ->where($map)
                ->order('l.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                //$v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $v['level1_name'] = $this->jjlevel1[$v['level1']];
                $list[$k] = $v;
            }

            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        $wallet_names = logic('Wallet')->getName();
        View::assign('wallet_names',$wallet_names);
        return View::fetch();
    }
}